package service

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto/rpc"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func PublishVideo(c *gin.Context) {
	var form request.PublishVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.PublishVideo(c, &__.PublishVideoReq{
		UserId: int64(form.UserId),
		Title:  form.Title,
		Image:  form.Image,
		Tags:   form.Tags,
		Refer:  form.Refer,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "发布失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "发布成功", "data": gin.H{
		"video": video.VideoId,
	}})
	return
}
func CheckVideo(c *gin.Context) {
	var form request.CheckVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.CheckVideo(c, &__.CheckVideoReq{
		UserId:  int64(form.UserId),
		VideoId: int64(form.VideoId),
		Status:  int64(form.Status),
		Reason:  form.Reason,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "审核失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "审核成功", "data": gin.H{
		"video": video.CheckId,
	}})
	return
}
func GetVideoList(c *gin.Context) {
	var form request.GetVideoList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.GetVideoList(c, &__.GetVideoListReq{
		UserId: int64(form.UserId),
		Page:   int64(form.Page),
		Size:   int64(form.Size),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询个性化推荐内容列表失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询个性化推荐内容列表成功", "data": gin.H{
		"video": video.List,
	}})
	return
}

func SearchVideoList(c *gin.Context) {
	var form request.SearchVideoList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.SearchVideoList(c, &__.SearchVideoListReq{
		AreaName:     form.AreaName,
		CategoryName: form.CategoryName,
		ChannelName:  form.ChannelName,
		YearName:     form.YearName,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "筛选查询失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "筛选查询成功", "data": gin.H{
		"video": video.List,
	}})
	return
}

func LookVideo(c *gin.Context) {
	var form request.LookVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.LookVideo(c, &__.LookVideoReq{
		UserId:  int64(form.UserId),
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "返回播放地址完成", "data": gin.H{
		"video": video.VideoUrl,
	}})
	return
}
